# 1.字典，有点像二维数组，带key的数组
alien_0 = {"color": "green", "points": 5}

print(alien_0['color'])
print(alien_0['points'])
print('\r')

# 2.给字典添加新的键值对
alien_0['x_position'] = 0
alien_0['y_position'] = 25

print(alien_0)
print('\r')

# 3.创建空字典，再添加键值对
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)
print('\r')

# 4.修改字典中的键值对，直接赋值修改
alien_0 = {'color': 'green'}
print("The alien is " + alien_0['color'] + ".")

alien_0['color'] = 'yellow'
print("The alien is now " + alien_0['color'] + ".")

# 5.删除键值对
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)

# 直接del键名
del alien_0['points']
print(alien_0)

# 6.遍历字典
user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi',
}
# for 可以获取到key和value，in 字典名.items()获取字典的元素 类似遍历列表
for key, value in user_0.items(): 
    print("\nKey: " + key) 
    print("Value: " + value)

# 7.单独遍历字典中的键
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
# keys() 获取字典的键
for name in favorite_languages.keys():
    print(name.title())

# 8.按顺序遍历字典中的所有键
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
# sorted给字典的键排序，然后再遍历
for name in sorted(favorite_languages.keys()):
    print(name.title() + ", thank you for taking the poll.")

# 9.遍历字典中所有的值
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
print("The following languages have been mentioned:")

# values 获取字典所有的值
for language in favorite_languages.values():
    print(language.title())

# 10.遍历字典中所有的值，并去重
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print("The following languages have been mentioned:") 

# set()去重字典的值
for language in set(favorite_languages.values()):
    print(language.title())
print("\r")

# 11.嵌套，列表里面嵌套字典
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
print(aliens)
#[{'color': 'green', 'points': 5}, {'color': 'yellow', 'points': 10}, {'color': 'red', 'points': 15}]

for alien in aliens:
    print(alien)
print("\r")

# 12.创建一个用于存储外星人的空列表
aliens = []
# 创建30个绿色的外星人
for alien_number in range(30):
    new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
    aliens.append(new_alien)

# 显示前五个外星人，[0:5] 0省略了
for alien in aliens[:5]:
    print(alien)
print("...")
# 显示创建了多少个外星人
print("Total number of aliens: " + str(len(aliens)))
print("\r")

# 13.字典里嵌套列表
# 存储所点比萨的信息
pizza = {
    'crust': 'thick',   
    'toppings': ['mushrooms', 'extra cheese'],
}
print(pizza)
# {'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese']}

# 概述所点的比萨
print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:")

for topping in pizza['toppings']:
    print("\t" + topping)
print("\r")

# 14.嵌套，字典套列表，双层for循环
favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c', 'php'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
}
for name, languages in favorite_languages.items():
    print("\n" + name.title() + "'s favorite languages are:")
    for language in languages:
        print("\t" + language.title())
print("=======================")

# 15.字典套字典
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
    },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
    },
}
for username, user_info in users.items():
    print("\nUsername: " + username) 
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']
    print("\tFull name: " + full_name.title())
    print("\tLocation: " + location.title())